#include "expresultdataframe.h"
#include "ui_expresultdataframe.h"
#include "algorithm.h"

ExpResultDataFrame::ExpResultDataFrame(QWidget *parent,Experiment* pExperiment) :
    QFrame(parent),
    ui(new Ui::ExpResultDataFrame)
{
    ui->setupUi(this);
    this->pExperiment=pExperiment;
}

ExpResultDataFrame::~ExpResultDataFrame()
{
    delete ui;
}

void ExpResultDataFrame::LoadData()
{
    on_tabWidget_Result_currentChanged(ui->tabWidget_Result->currentIndex());
}

void ExpResultDataFrame::on_tabWidget_Result_currentChanged(int index)
{
    switch(index)
    {
        case 0:
            LoadDataCalCn();
        break;
        case 1:
            LoadDataCalSps();
        break;
        case 2:
            LoadDataJammingCps();
        break;
        case 3:
            LoadDataIsotope();
        break;
        case 4:
            LoadDataHalfCn();
        break;
    }
}

void ExpResultDataFrame::LoadDataCalCn()
{
    ui->tableWidget_CnCal->clear();
    ui->tableWidget_CnCal->setRowCount(0);

    QStringList cpsCalHeader=QStringList()<<"样本名称"<<"运行次序";
    QStringList scanElement;
    QList<ScanPara> scanParas;
    pExperiment->GetScanPara(scanParas,1);
    for(int i=0;i<scanParas.count();i++)
    {
        scanElement.append(scanParas.at(i).name);
    }
    cpsCalHeader.append(scanElement);
    ui->tableWidget_CnCal->setColumnCount(cpsCalHeader.count());
    ui->tableWidget_CnCal->setHorizontalHeaderLabels(cpsCalHeader);

    int row=0;
    pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> sampleInfors=pExpSample->GetSampleInfors();
    for(int i=0;i<sampleInfors.count();i++)
    {
        SampleInfor sampleInfor=sampleInfors.at(i);
        for(int j=0;j<sampleInfor.mainScanCount;j++)
        {
            QHash<QString,double> datas=sampleInfor.GetMainScanResultPtr(j)->GetCnCals();
            if(datas.count()<=0) break;

            ui->tableWidget_CnCal->insertRow(ui->tableWidget_CnCal->rowCount());
            QTableWidgetItem *pItem;
            pItem=new QTableWidgetItem;
            pItem->setText(sampleInfor.sampleName);//
            ui->tableWidget_CnCal->setItem(row,0,pItem);
            pItem=new QTableWidgetItem;
            pItem->setText(QString::number(j));//
            ui->tableWidget_CnCal->setItem(row,1,pItem);

            for(int column=2;column<cpsCalHeader.count();column++)
            {
                QString name=cpsCalHeader.at(column);
                QTableWidgetItem *pItem;
                pItem=new QTableWidgetItem;
                pItem->setText(QString::number(datas.value(name)));//
                ui->tableWidget_CnCal->setItem(row,column,pItem);
            }
            row++;
        }
        //rsd
        QHash<QString,double> datas=sampleInfor.GetRsdCnCals();
        if(datas.count()<=0) continue;
        ui->tableWidget_CnCal->insertRow(ui->tableWidget_CnCal->rowCount());
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setText("RSD(%)");//
        ui->tableWidget_CnCal->setItem(row,1,pItem);
        for(int column=2;column<cpsCalHeader.count();column++)
        {
            QString name=cpsCalHeader.at(column);
            QTableWidgetItem *pItem;
            pItem=new QTableWidgetItem;
            pItem->setText(QString::number(datas.value(name)));//
            ui->tableWidget_CnCal->setItem(row,column,pItem);
        }
        row++;
    }
}
void ExpResultDataFrame::LoadDataCalSps()
{
    ui->tableWidget_CpsCal->clear();
    ui->tableWidget_CpsCal->setRowCount(0);

    QStringList cpsCalHeader=QStringList()<<"样本名称"<<"运行次序";
    QStringList scanElement;
    QList<ScanPara> scanParas;
    pExperiment->GetScanPara(scanParas,1);
    for(int i=0;i<scanParas.count();i++)
    {
        scanElement.append(scanParas.at(i).name);
    }
    cpsCalHeader.append(scanElement);
    ui->tableWidget_CpsCal->setColumnCount(cpsCalHeader.count());
    ui->tableWidget_CpsCal->setHorizontalHeaderLabels(cpsCalHeader);

    int row=0;
    pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> sampleInfors=pExpSample->GetSampleInfors();
    for(int i=0;i<sampleInfors.count();i++)
    {
        SampleInfor sampleInfor=sampleInfors.at(i);
        for(int j=0;j<sampleInfor.mainScanCount;j++)
        {
            QHash<QString,double> datas=sampleInfor.GetMainScanResultPtr(j)->GetCpsCals();
            if(datas.count()<=0) break;

            ui->tableWidget_CpsCal->insertRow(ui->tableWidget_CpsCal->rowCount());
            QTableWidgetItem *pItem;
            pItem=new QTableWidgetItem;
            pItem->setText(sampleInfor.sampleName);//
            ui->tableWidget_CpsCal->setItem(row,0,pItem);
            pItem=new QTableWidgetItem;
            pItem->setText(QString::number(j));//
            ui->tableWidget_CpsCal->setItem(row,1,pItem);

            for(int column=2;column<cpsCalHeader.count();column++)
            {
                QString name=cpsCalHeader.at(column);
                QTableWidgetItem *pItem;
                pItem=new QTableWidgetItem;
                pItem->setText(QString::number(datas.value(name)));//
                ui->tableWidget_CpsCal->setItem(row,column,pItem);
            }
            row++;
        }
        //rsd
        QHash<QString,double> datas=sampleInfor.GetRsdCpsCals();
        if(datas.count()<=0) continue;
        ui->tableWidget_CpsCal->insertRow(ui->tableWidget_CpsCal->rowCount());
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setText("RSD(%)");//
        ui->tableWidget_CpsCal->setItem(row,1,pItem);
        for(int column=2;column<cpsCalHeader.count();column++)
        {
            QString name=cpsCalHeader.at(column);
            QTableWidgetItem *pItem;
            pItem=new QTableWidgetItem;
            pItem->setText(QString::number(datas.value(name)));//
            ui->tableWidget_CpsCal->setItem(row,column,pItem);
        }
        row++;
    }
}

void ExpResultDataFrame::LoadDataJammingCps()
{
    ui->tableWidget_CpsJamming->clear();
    ui->tableWidget_CpsJamming->setRowCount(0);

    QStringList cpsCalHeader=QStringList()<<"样本名称"<<"运行次序";
    QStringList scanElement;
    QList<ScanPara> scanParas;
    pExperiment->GetScanPara(scanParas,1);
    for(int i=0;i<scanParas.count();i++)
    {
        scanElement.append(scanParas.at(i).name);
    }
    cpsCalHeader.append(scanElement);
    ui->tableWidget_CpsJamming->setColumnCount(cpsCalHeader.count());
    ui->tableWidget_CpsJamming->setHorizontalHeaderLabels(cpsCalHeader);

    int row=0;
    pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> sampleInfors=pExpSample->GetSampleInfors();
    for(int i=0;i<sampleInfors.count();i++)
    {
        SampleInfor sampleInfor=sampleInfors.at(i);
        for(int j=0;j<sampleInfor.mainScanCount;j++)
        {
            QHash<QString,double> datas=sampleInfor.GetMainScanResultPtr(j)->GetCpsJammings();
            if(datas.count()<=0) break;

            ui->tableWidget_CpsJamming->insertRow(ui->tableWidget_CpsJamming->rowCount());
            QTableWidgetItem *pItem;
            pItem=new QTableWidgetItem;
            pItem->setText(sampleInfor.sampleName);//
            ui->tableWidget_CpsJamming->setItem(row,0,pItem);
            pItem=new QTableWidgetItem;
            pItem->setText(QString::number(j));//
            ui->tableWidget_CpsJamming->setItem(row,1,pItem);

            for(int column=2;column<cpsCalHeader.count();column++)
            {
                QString name=cpsCalHeader.at(column);
                QTableWidgetItem *pItem;
                pItem=new QTableWidgetItem;
                pItem->setText(QString::number(datas.value(name)));//
                ui->tableWidget_CpsJamming->setItem(row,column,pItem);
            }
            row++;
        }
        //rsd
        QHash<QString,double> datas=sampleInfor.GetRsdCpsJammings();
        if(datas.count()<=0) continue;
        ui->tableWidget_CpsJamming->insertRow(ui->tableWidget_CpsJamming->rowCount());
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setText("RSD(%)");//
        ui->tableWidget_CpsJamming->setItem(row,1,pItem);
        for(int column=2;column<cpsCalHeader.count();column++)
        {
            QString name=cpsCalHeader.at(column);
            QTableWidgetItem *pItem;
            pItem=new QTableWidgetItem;
            pItem->setText(QString::number(datas.value(name)));//
            ui->tableWidget_CpsJamming->setItem(row,column,pItem);
        }
        row++;
    }
}
void ExpResultDataFrame::LoadDataIsotope()
{
    ui->tableWidget_Isotope->clear();
    ui->tableWidget_Isotope->setRowCount(0);

    QStringList cpsCalHeader=QStringList()<<"样本名称"<<"运行次序";
    QStringList scanElement;
    IsotopePara *pIsotopePara=pExperiment->GetIsotopeParaPtr();
    QList<QPair<QString,QString> > isotopeRatios=pIsotopePara->GetIsotopePara();
    for(int i=0;i<isotopeRatios.count();i++)
    {
        scanElement.append(isotopeRatios.at(i).first+"/"+isotopeRatios.at(i).second);
    }
    cpsCalHeader.append(scanElement);
    ui->tableWidget_Isotope->setColumnCount(cpsCalHeader.count());
    ui->tableWidget_Isotope->setHorizontalHeaderLabels(cpsCalHeader);

    int row=0;
    pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> sampleInfors=pExpSample->GetSampleInfors();
    for(int i=0;i<sampleInfors.count();i++)
    {
        SampleInfor sampleInfor=sampleInfors.at(i);
        for(int j=0;j<sampleInfor.mainScanCount;j++)
        {
            QHash<QString,double> datas=sampleInfor.GetMainScanResultPtr(j)->GetIsotopes();
            if(datas.count()<=0) break;

            ui->tableWidget_Isotope->insertRow(ui->tableWidget_Isotope->rowCount());
            QTableWidgetItem *pItem;
            pItem=new QTableWidgetItem;
            pItem->setText(sampleInfor.sampleName);//
            ui->tableWidget_Isotope->setItem(row,0,pItem);
            pItem=new QTableWidgetItem;
            pItem->setText(QString::number(j));//
            ui->tableWidget_Isotope->setItem(row,1,pItem);

            for(int column=2;column<cpsCalHeader.count();column++)
            {
                QString name=cpsCalHeader.at(column);
                QTableWidgetItem *pItem;
                pItem=new QTableWidgetItem;
                pItem->setText(QString::number(datas.value(name)));//
                ui->tableWidget_Isotope->setItem(row,column,pItem);
            }
            row++;
        }
        //rsd
        QHash<QString,double> datas=sampleInfor.GetRsdIsotopes();
        if(datas.count()<=0) continue;
        ui->tableWidget_Isotope->insertRow(ui->tableWidget_Isotope->rowCount());
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setText("RSD(%)");//
        ui->tableWidget_Isotope->setItem(row,1,pItem);
        for(int column=2;column<cpsCalHeader.count();column++)
        {
            QString name=cpsCalHeader.at(column);
            QTableWidgetItem *pItem;
            pItem=new QTableWidgetItem;
            pItem->setText(QString::number(datas.value(name)));//
            ui->tableWidget_Isotope->setItem(row,column,pItem);
        }
        row++;
    }
}
void ExpResultDataFrame::LoadDataHalfCn()
{
    ui->tableWidget_CnHalf->clear();
    ui->tableWidget_CnHalf->setRowCount(0);

    QStringList cpsCalHeader=QStringList()<<"样本名称"<<"运行次序";
    QStringList scanElement;
    QList<ScanPara> scanParas;
    ExpConllectPara *pExpConllectPara=pExperiment->GetExpConllectParaPtr();
    pExperiment->GetScanPara(scanParas,0);
//    for(int i=0;i<scanParas.count();i++)
//    {
//        scanElement.append(scanParas.at(i).name);
//    }
//    cpsCalHeader.append(scanElement);
    ui->tableWidget_CnHalf->setColumnCount(cpsCalHeader.count());
    ui->tableWidget_CnHalf->setHorizontalHeaderLabels(cpsCalHeader);
    if(pExpConllectPara->bPreScan==false) return;

    int row=0;
    pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> sampleInfors=pExpSample->GetSampleInfors();
    for(int i=0;i<sampleInfors.count();i++)
    {
        SampleInfor sampleInfor=sampleInfors.at(i);
        for(int j=0;j<sampleInfor.preScanCount;j++)
        {
            QHash<QString,double> datas=sampleInfor.GetPreScanResultPtr(j)->GetCnHalfs();
            if(datas.count()<=0) break;

            ui->tableWidget_CnHalf->insertRow(ui->tableWidget_CnHalf->rowCount());
            QTableWidgetItem *pItem;
            pItem=new QTableWidgetItem;
            pItem->setText(sampleInfor.sampleName);//
            ui->tableWidget_CnHalf->setItem(row,0,pItem);
            pItem=new QTableWidgetItem;
            pItem->setText(QString::number(j));//
            ui->tableWidget_CnHalf->setItem(row,1,pItem);

            for(int column=2;column<cpsCalHeader.count();column++)
            {
                QString name=cpsCalHeader.at(column);
                QTableWidgetItem *pItem;
                pItem=new QTableWidgetItem;
                pItem->setText(QString::number(datas.value(name)));//
                ui->tableWidget_CnHalf->setItem(row,column,pItem);
            }
            row++;
        }
    }
}


